<?php
class category extends My_Controller
{
	function category()
	{
		parent::__construct();
	}
	
	function index($name_category, $id_category)
	{
		$category			= $this->db->get_where('categories', array('id'=>$id_category))->row_array();
		if (!empty($category['id']) && isset($category['id']))
		{
			$data['id_cat']					= $id_category;
			$data['name_cat']				= $name_category;
			
			$data['id_subcat']				= "";
			
			$data['subcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_category), '', '', array());
			$data['totalproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_1_id'=>$id_category), '', '', array());
			$data['title']					= $name_category;
			
			$this->load->library('pagination');
			$per_page 						= 3;
			$offset							= $this->uri->segment(2);
			$total 	  						= $this->mod_lib->count('products', array('cat_1_id'=>$id_category));
			$data['listproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_1_id'=>$id_category), $per_page, $offset, array());
			$base_url						= site_url($name_category.'-'.$id_category.'.html');
			$config['base_url']				= $base_url;
			$config['per_page']				= $per_page;
			$config['total_rows']			= $total;
			$config['uri_segment']			= 2;
			$data['rs']						= $this->uri->segment(2);
			$this->pagination->initialize($config);
			$data['paging']					= $this->pagination->create_links();
			$this->load->view('sites/category/list_product_category', $data);
		}
		else
		{
			redirect(base_url());
		}
	}
	
	function category_province($name_province, $name_category, $id_category)
	{		
		$category			= $this->db->get_where('categories', array('id'=>$id_category))->row_array();
		if (!empty($category['id']) && isset($category['id']))
		{
			$data['id_cat']					= $id_category;
			$data['name_cat']				= $name_category;
				
			$data['id_subcat']				= "";
				
			$province_id		= "";
			$province			= $this->mod_lib->load_all('provinces', '', array('status'=>1), '', '', array('hot'=>'DESC', 'name'=>'ASC'));
			foreach ($province as $prov)
			{
				if ($name_province	== url_title(utf8_to_ascii($prov['name'])))
				{
					$province_id	= $prov['id'];						
				}
			}
			
			$data['province_id']			= $province_id;
			$data['province_name']			= $name_province;
			$data['subcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_category), '', '', array());
			$data['totalproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_1_id'=>$id_category, 'province_id'=>$province_id), '', '', array());
			$data['title']					= $name_category;
		
			$this->load->library('pagination');
			$per_page 						= 3;
			$offset							= $this->uri->segment(3);
			$total 	  						= $this->mod_lib->count('products', array('cat_1_id'=>$id_category, 'province_id'=>$province_id));
			$data['listproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_1_id'=>$id_category, 'province_id'=>$province_id), $per_page, $offset, array());
			$base_url						= site_url($name_province.'/'.$name_category.'-'.$id_category.'.html');
			$config['base_url']				= $base_url;
			$config['per_page']				= $per_page;
			$config['total_rows']			= $total;
			$config['uri_segment']			= 3;
			$data['rs']						= $this->uri->segment(3);
			$this->pagination->initialize($config);
			$data['paging']					= $this->pagination->create_links();
			$this->load->view('sites/category/list_product_category', $data);
		}
		else
		{
			redirect(base_url());
		}		
	}
	
	function subcategory($name_subcat, $name_cat, $id_cat, $id_subcat)
	{
		$category						= $this->db->get_where('categories', array('id'=>$id_cat))->row_array();
		$data['id_cat']					= $id_cat;
		$data['name_cat']				= url_title(utf8_to_ascii($category['name']));
		
		$subcategory					= $this->db->get_where('categories', array('id'=>$id_subcat))->row_array();
		$data['id_subcat']				= $id_subcat;
		$data['name_subcat']			= url_title(utf8_to_ascii($subcategory['name']));
		
		$data['id_subsubcat']			= "";
		
		$data['title']					= $name_subcat.'-'.$name_cat;
		$data['subcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_cat), '', '', array());
		$data['subsubcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_subcat), '', '', array());
		$data['totalproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_1_id'=>$id_cat), '', '', array());
		$data['totalproductsubcat']		= $this->mod_lib->load_all('products', '', array('cat_2_id'=>$id_subcat), '', '', array());
		
		$this->load->library('pagination');
		$per_page 						= 3;
		$offset							= $this->uri->segment(2);
		$total 	  						= $this->mod_lib->count('products', array('cat_2_id'=>$id_subcat));
		$data['listproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_2_id'=>$id_subcat), $per_page, $offset, array());
		$base_url						= site_url($name_subcat.'-'.$name_cat.'-'.$id_cat.'-'.$id_subcat.'.html');
		$config['base_url']				= $base_url;
		$config['per_page']				= $per_page;
		$config['total_rows']			= $total;
		$config['uri_segment']			= 2;
		$data['rs']						= $this->uri->segment(2);
		$this->pagination->initialize($config);
		$data['paging']					= $this->pagination->create_links();
		
		$this->load->view('sites/category/list_product_category', $data);
	}
	
	function province_subcategory($name_province, $name_subcat, $name_cat, $id_cat, $id_subcat)
	{
		$category						= $this->db->get_where('categories', array('id'=>$id_cat))->row_array();
		$data['id_cat']					= $id_cat;
		$data['name_cat']				= url_title(utf8_to_ascii($category['name']));
		
		$subcategory					= $this->db->get_where('categories', array('id'=>$id_subcat))->row_array();
		$data['id_subcat']				= $id_subcat;
		$data['name_subcat']			= url_title(utf8_to_ascii($subcategory['name']));
		
		$data['id_subsubcat']			= "";
		
		$province_id		= "";
		$province			= $this->mod_lib->load_all('provinces', '', array('status'=>1), '', '', array('hot'=>'DESC', 'name'=>'ASC'));
		foreach ($province as $prov)
		{
			if ($name_province	== url_title(utf8_to_ascii($prov['name'])))
			{
				$province_id	= $prov['id'];
			}
		}
			
		$data['province_id']			= $province_id;
		$data['province_name']			= $name_province;
		
		$data['title']					= $name_subcat.'-'.$name_cat;
		$data['subcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_cat), '', '', array());
		$data['subsubcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_subcat), '', '', array());
		$data['totalproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_1_id'=>$id_cat, 'province_id'=>$province_id), '', '', array());
		$data['totalproductsubcat']		= $this->mod_lib->load_all('products', '', array('cat_2_id'=>$id_subcat, 'province_id'=>$province_id), '', '', array());
		
		$this->load->library('pagination');
		$per_page 						= 3;
		$offset							= $this->uri->segment(3);
		$total 	  						= $this->mod_lib->count('products', array('cat_2_id'=>$id_subcat, 'province_id'=>$province_id));
		$data['listproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_2_id'=>$id_subcat, 'province_id'=>$province_id), $per_page, $offset, array());
		$base_url						= site_url($name_province.'/'.$name_subcat.'-'.$name_cat.'-'.$id_cat.'-'.$id_subcat.'.html');
		$config['base_url']				= $base_url;
		$config['per_page']				= $per_page;
		$config['total_rows']			= $total;
		$config['uri_segment']			= 3;
		$data['rs']						= $this->uri->segment(3);
		$this->pagination->initialize($config);
		$data['paging']					= $this->pagination->create_links();
	
		$this->load->view('sites/category/list_product_category', $data);
	}
	
	function subsubcategory($name_subsubcat, $name_subcat, $name_cat, $id_cat, $id_subcat, $id_subsubcat)
	{
		$category						= $this->db->get_where('categories', array('id'=>$id_cat))->row_array();
		$data['id_cat']					= $id_cat;
		$data['name_cat']				= url_title(utf8_to_ascii($category['name']));
		
		$subcategory					= $this->db->get_where('categories', array('id'=>$id_subcat))->row_array();
		$data['id_subcat']				= $id_subcat;
		$data['name_subcat']			= url_title(utf8_to_ascii($subcategory['name']));
		
		$subsubcategory					= $this->db->get_where('categories', array('id'=>$id_subsubcat))->row_array();
		$data['id_subsubcat']			= $id_subsubcat;		
		
		$data['title']					= $name_subsubcat.'-'.$name_subcat.'-'.$name_cat;
		$data['subcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_cat), '', '', array());
		$data['subsubcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_subcat), '', '', array());
		$data['totalproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_2_id'=>$id_subcat), '', '', array());
		$data['totalproductsubcat']		= $this->mod_lib->load_all('products', '', array('cat_2_id'=>$id_subcat), '', '', array());
		
		$this->load->library('pagination');
		$per_page 						= 3;
		$offset							= $this->uri->segment(2);
		$total 	  						= $this->mod_lib->count('products', array('cat_3_id'=>$id_subsubcat));
		$data['listproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_3_id'=>$id_subsubcat), $per_page, $offset, array());
		$base_url						= site_url($name_subsubcat.'-'.$name_subcat.'-'.$name_cat.'-'.$id_cat.'-'.$id_subcat.'-'.$id_subsubcat.'.html');
		$config['base_url']				= $base_url;
		$config['per_page']				= $per_page;
		$config['total_rows']			= $total;
		$config['uri_segment']			= 2;
		$data['rs']						= $this->uri->segment(2);
		$this->pagination->initialize($config);
		$data['paging']					= $this->pagination->create_links();
		
		$this->load->view('sites/category/list_product_category', $data);
	}
	
	function province_subsubcategory($name_province, $name_subsubcat, $name_subcat, $name_cat, $id_cat, $id_subcat, $id_subsubcat)
	{
		$category						= $this->db->get_where('categories', array('id'=>$id_cat))->row_array();
		$data['id_cat']					= $id_cat;
		$data['name_cat']				= url_title(utf8_to_ascii($category['name']));
		
		$subcategory					= $this->db->get_where('categories', array('id'=>$id_subcat))->row_array();
		$data['id_subcat']				= $id_subcat;
		$data['name_subcat']			= url_title(utf8_to_ascii($subcategory['name']));
		
		$subsubcategory					= $this->db->get_where('categories', array('id'=>$id_subsubcat))->row_array();
		$data['id_subsubcat']			= $id_subsubcat;
		$data['name_subsubcat']			= url_title(utf8_to_ascii($subsubcategory['name']));;
		
		/*
		$data['subcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_subsubcat), '', '', array());
		$data['totalproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_3_id'=>$id_subsubcat), '', '', array());
		*/
	
		$province_id		= "";
		$province			= $this->mod_lib->load_all('provinces', '', array('status'=>1), '', '', array('hot'=>'DESC', 'name'=>'ASC'));
		foreach ($province as $prov)
		{
			if ($name_province	== url_title(utf8_to_ascii($prov['name'])))
			{
				$province_id	= $prov['id'];
			}
		}
			
		$data['province_id']			= $province_id;
		$data['province_name']			= $name_province;
		
		$data['title']					= $name_subsubcat.'-'.$name_subcat.'-'.$name_cat;
		$data['subcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_cat), '', '', array());
		$data['subsubcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_subcat), '', '', array());
		$data['totalproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_2_id'=>$id_subcat, 'province_id'=>$province_id), '', '', array());
		$data['totalproductsubcat']		= $this->mod_lib->load_all('products', '', array('cat_2_id'=>$id_subcat, 'province_id'=>$province_id), '', '', array());
		
		$this->load->library('pagination');
		$per_page 						= 3;
		$offset							= $this->uri->segment(3);
		$total 	  						= $this->mod_lib->count('products', array('cat_3_id'=>$id_subsubcat, 'province_id'=>$province_id));
		$data['listproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_3_id'=>$id_subsubcat, 'province_id'=>$province_id), $per_page, $offset, array());
		$base_url						= site_url($name_province.'/'.$name_subsubcat.'-'.$name_subcat.'-'.$name_cat.'-'.$id_cat.'-'.$id_subcat.'-'.$id_subsubcat.'.html');
		$config['base_url']				= $base_url;
		$config['per_page']				= $per_page;
		$config['total_rows']			= $total;
		$config['uri_segment']			= 3;
		$data['rs']						= $this->uri->segment(3);
		$this->pagination->initialize($config);
		$data['paging']					= $this->pagination->create_links();
	
		$this->load->view('sites/category/list_product_category', $data);
	}
	
	function distinct_category($name_province, $name_category, $id_category, $distinct)
	{
		$category			= $this->db->get_where('categories', array('id'=>$id_category))->row_array();
		if (!empty($category['id']) && isset($category['id']))
		{
			$data['id_cat']					= $id_category;
			$data['name_cat']				= $name_category;
				
			$data['id_subcat']				= "";
				
			$province_id		= "";
			$province			= $this->mod_lib->load_all('provinces', '', array('status'=>1), '', '', array('hot'=>'DESC', 'name'=>'ASC'));
			foreach ($province as $prov)
			{
				if ($name_province	== url_title(utf8_to_ascii($prov['name'])))
				{
					$province_id	= $prov['id'];						
				}
			}
			
			$distinct_id					= explode('d', $distinct);
			
			$data['province_id']			= $province_id;
			$data['province_name']			= $name_province;
			$data['subcategory']			= $this->mod_lib->load_all('categories', '', array('cat_id'=>$id_category), '', '', array());
			$data['totalproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_1_id'=>$id_category, 'province_id'=>$province_id, 'distinct_id'=>$distinct_id[1]), '', '', array());
			$data['title']					= $name_category;
		
			$this->load->library('pagination');
			$per_page 						= 3;
			$offset							= $this->uri->segment(3);
			$total 	  						= $this->mod_lib->count('products', array('cat_1_id'=>$id_category, 'province_id'=>$province_id, 'distinct_id'=>$distinct_id[1]));
			$data['listproductcategory']	= $this->mod_lib->load_all('products', '', array('cat_1_id'=>$id_category, 'province_id'=>$province_id, 'distinct_id'=>$distinct_id[1]), $per_page, $offset, array());
			$base_url						= site_url($name_province.'/'.$name_category.'-'.$id_category.'.html');
			$config['base_url']				= $base_url;
			$config['per_page']				= $per_page;
			$config['total_rows']			= $total;
			$config['uri_segment']			= 3;
			$data['rs']						= $this->uri->segment(3);
			$this->pagination->initialize($config);
			$data['paging']					= $this->pagination->create_links();
			$this->load->view('sites/category/list_product_category', $data);
		}
		else
		{
			redirect(base_url());
		}
	}
}